#include <iostream>
#include "cpp-httplib/httplib.h"

using namespace std;

int main()
{
    httplib::Server server;
    // 设置 Keep-Alive 超时时间为 5 秒（示例）
    server.set_keep_alive_timeout(2);


    //注册函数
    //get获取内容,post上传文件或表单
    server.Get("/hi!",[&](const httplib::Request& req,httplib::Response& res)
    {
        cout<<"test\n";
        cout<<req.local_addr;
        res.set_content("hello!","text/plain");//设置内容及内容格式
        res.status=200;
    });
    //支持正则表达式
    server.Get(R"(/numbers/(\d+))",[&](const httplib::Request& req,httplib::Response& res) {
        auto numbers = req.matches[1];
        res.set_content(numbers, "text/plain");
        res.status=200;
    });

    server.Post("/upload",[&](const httplib::Request& req,httplib::Response& res){
        size_t size=req.files.size();//获取文件数量
        bool ret=req.has_file("test.txt");//是否存在该文件

        if(ret==true)
        {
            auto val=req.get_file_value("test");//获取该文件内容
            cout<<val.filename<<endl;
            cout<<val.content_type<<':'<<val.content<<endl;
            cout<<val.name<<endl;
        }
    });

    server.listen("0.0.0.0",9090);
    return 0;
}